package com.poe.controller.user;

import com.poe.entity.User;
import com.poe.service.iuser.UserService;
import com.poe.vo.UserReturn;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;

@Controller
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    UserReturn userReturn;
    @RequestMapping("/regin")
    @ResponseBody
    public void regin(HttpServletResponse response,User user) throws IOException{
        boolean bl=userService.regin(user);
        //返回真或假
        UserReturn userReturn=new UserReturn();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter pw=response.getWriter();
        if(bl){
            userReturn.setNum(1);
        }else{
            userReturn.setNum(0);
        }
        String a=JSONObject.fromObject(userReturn).toString();
        pw.write(a);
        pw.flush();
        pw.close();
    }
    //---------------------登录
    @RequestMapping("login")
    @ResponseBody
    public void login(User user, HttpServletResponse response, HttpSession session) throws IOException{
        User user1=userService.login(user);
        UserReturn userReturn=new UserReturn();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter pw=response.getWriter();
        if(null!=user1){
            userReturn.setNum(1);
            session.setAttribute("user1", user1);
            ServletContext application = session.getServletContext();
            // 在application范围由一个HashSet集保存所有的session
            HashSet sessions = (HashSet) application.getAttribute("sessions");
            if(sessions == null){
                sessions = new HashSet();
                application.setAttribute("sessions",sessions);
            }
            // 新创建的session均添加到HashSet集中
            sessions.add(session);
            // 可以在别处从application范围中取出sessions集合

            // 然后使用sessions.size()获取当前活动的session数，即为“在线人数”

        }else{
            userReturn.setNum(0);
        }
        String a=JSONObject.fromObject(userReturn).toString();
        pw.write(a);
        pw.flush();
        pw.close();

    }
    //---------------------查询用户名
    @RequestMapping("userName")
    @ResponseBody
    public void userName(String userName,HttpServletResponse response) throws IOException{
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter pw=response.getWriter();
        User user2=userService.findUserName(userName);
        String a=JSONObject.fromObject(user2).toString();
        pw.write(a);
        pw.flush();
        pw.close();

    }
//注销

    @RequestMapping("loginOut")
    public String loginOut(HttpSession session) throws IOException{
        session.setAttribute("user1", null);
        ServletContext application = session.getServletContext();
        HashSet sessions = (HashSet) application.getAttribute("sessions");
        sessions.remove(session);
        return "index";
    }
    @RequestMapping("toLogin")
    public String toLogin(){
        return "commons/login";
    }
    @RequestMapping("toRegin")
    public String toRegin(){
        return "commons/regin";
    }
}
